package com.juguo.charginganimation.View.HomeFragment;

import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.juguo.charginganimation.R;
import com.juguo.charginganimation.utils.UITools;
import com.juguo.charginganimation.View.gallery.PopularFragmentGallery;
import com.juguo.charginganimation.Viewmodel.PopularViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class PopularFragment extends Fragment {

    private PopularViewModel mViewModel;
    private Context mContext;
    private ViewPager viewPagers;

    public static PopularFragment newInstance() {
        return new PopularFragment();
    }

    @Override
    public void onAttach(@NonNull @NotNull Context context) {
        super.onAttach(context);
        mContext = context;
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.popular_fragment, container, false);
        viewPagers = root.findViewById(R.id.viewpages);
        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(PopularViewModel.class);
        //修复标题栏与状态栏重叠
        UITools.fitTitleBar(getActivity(), getView());
        initViewPage();
    }

    /**
     * viewPage初始化
     */
    private void initViewPage() {

        //fragment数据源
        final ArrayList<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new PopularFragmentGallery());
        viewPagers.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        //预加载界面
        viewPagers.setOffscreenPageLimit(fragmentList.size() - 1);
    }

}